package com.zichuan.message;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author zichuan
 * @version 1.0
 * @Package : com.zichuan.message
 * @Description : TODO
 * @Create on : 2025/2/17 19:48
 */
@Slf4j
@Component
public class ReceiveMessage {

    @RabbitListener(queues = {"queue.fanout.a", "queue.fanout.b"})
    public void ReceiveMessage(Message message, Channel channel){
        MessageProperties messageProperties = message.getMessageProperties();
        //获取消息的唯一标识
        long deliveryTag = messageProperties.getDeliveryTag();
        try {
            byte[] body = message.getBody();
            String msg = new String(body);
            log.info("接受到的消息：{}",msg);
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            try {
                channel.basicNack(deliveryTag,false,true);
                //channel.basicReject(deliveryTag,false);//拒绝消息
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException("");
        }

    }

}
